﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace SystemUpdate
{
    public partial class SystemUpdateForm : Form
    {
        const string REGISTRY_SETTINGS_KEY   = @"\Software\Microsoft\WBT\Monitor";
        const string REGISTRY_SETTINGS_VALUE = @"Desktop Area Frequency"; 

        public DownloadFrom downloadFrom;
        private UpdateSettings settings;

        public UpdateSettings Settings
        {
            get { return settings; }
            set { settings = value; }
        }

        public SystemUpdateForm()
        {
            settings = UpdateSettings.ReadSetting();
            InitializeComponent();

            string sHeight, sWidth;
            int height, width;

            try
            {
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(REGISTRY_SETTINGS_KEY, true))
                {
                    string[] values = key.GetValue(REGISTRY_SETTINGS_VALUE).ToString().Split( new char[] {'x','X','@'});
                    height = Int32.Parse(values[1].Trim());
                    width  = Int32.Parse(values[0].Trim());
                }
            }
            catch ( Exception ex) { 
                height = 600;
                width  = 800;
            }

            this.Location = new Point((width - this.Size.Width) / 2, (height - this.Size.Height) / 2);

            this.MainPanel.Controls.Add(new SystemUpdate.Step1(settings));

            if (settings.DownloadFromFTP || settings.DownloadFromHTTP || settings.DownloadFromUSB)
            {
                this.Next.Enabled = true;
            }
        }

        private void Next_Click(object sender, EventArgs e)
        {
            if (this.MainPanel.Controls[0] is SystemUpdate.Step1)
            {
                if (this.settings.DownloadFromFTP)
                {
                    this.MainPanel.Controls.Add(new SystemUpdate.Step2FTP());
                }
                else if (this.settings.DownloadFromHTTP)
                {
                    this.MainPanel.Controls.Add(new SystemUpdate.Step2WWW());
                }
                else
                {
                    this.MainPanel.Controls.Add(new SystemUpdate.Step2USBFlash());
                }
                this.MainPanel.Controls.RemoveAt(0);
                this.Back.Enabled = true;
            }
            else if (this.MainPanel.Controls[0] is SystemUpdate.Step3)
            { }
            else
            {
                try
                {
                    downloadFrom.CheckConnection();

                    this.MainPanel.Controls.Add(new SystemUpdate.Step3());
                    this.MainPanel.Controls.RemoveAt(0);
                    this.Back.Enabled = false;
                    this.Next.Enabled = false;
                    downloadFrom.Download();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }
        }

        private void Back_Click(object sender, EventArgs e)
        {
            if (this.MainPanel.Controls[0] is SystemUpdate.Step1)
            { }
            else if (this.MainPanel.Controls[0] is SystemUpdate.Step3)
            { }
            else
            {
                this.Back.Enabled = false;
                this.Next.Enabled = false;
                this.MainPanel.Controls.Add(new SystemUpdate.Step1(settings));
                this.MainPanel.Controls.RemoveAt(0);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            if (this.downloadFrom != null)
            {
                this.downloadFrom.CancelDownloading();
            }

            this.Close();
        }

        private void SystemUpdateForm_Closed(object sender, EventArgs e)
        {
            this.Settings.SaveSettings();
        }
    }
}
